package com.lijian.search.service.impl;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lijian.common.utils.TaotaoResult;
import com.lijian.search.mapper.ItemMapper;
import com.lijian.search.pojo.SearchItem;
import com.lijian.search.service.ItemService;

/**
 * <p>Title: ItemServiceImpl.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * @author lijian
 * @date 2017年4月16日下午8:39:58
 * @version 1.0
 */
@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private SolrServer solrServer;
	@Autowired
	private ItemMapper itemMapper;
	/**
	 * <p>Title: importItems</p>
	 * <p>Description: 获取商品列表，导入到solr</p>
	 * @return
	 * @throws IOException 
	 * @throws SolrServerException 
	 * @see com.lijian.search.service.ItemService#importItems()
	 */
	@Override
	public TaotaoResult importItems() throws SolrServerException, IOException {
		List<SearchItem> itemList = itemMapper.getItemList();
		SolrInputDocument doc=null;
		for(SearchItem item:itemList){
			doc=new SolrInputDocument();
			doc.addField("id", item.getId());
			doc.addField("item_title", item.getTitle());
			doc.addField("item_price", item.getPrice());
			doc.addField("item_sell_point", item.getSell_point());
			doc.addField("item_image", item.getImage());
			doc.addField("item_category_name", item.getCategory_name());
			doc.addField("item_desc", item.getItem_desc());
			
			//写入索引库
			solrServer.add(doc);
		}
		solrServer.commit();
		return TaotaoResult.ok();
	}

}
